package com.yyhh.helpcall.http;

import com.yyhh.helpcall.BuildConfig;
import com.yyhh.helpcall.http.api.Api;
import com.yyhh.helpcall.http.interceptor.AuthInterceptor;
import com.yyhh.helpcall.http.interceptor.PublicParamsInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class OkHttpUtils {

    //http://xuleleapp.yqtyy.com这个是测试的域名，正式的域名是  http://39.106.10.5
    // http://ttzlsm.rongjinke.com/
//    public static final String BASE_URL = "http://ttzlsm.rongjinke.com/";
//    public static final String BASE_URL = "http://bjbp.rongjinke.com/";
//    public static final String BASE_URL = "http://jnqc.rongjinke.com/";
//    public static final String BASE_URL = "http://ty.rongjinke.com/";
    public static final String BASE_URL = "http://tyxt.rongjinke.com/";

    private static class OkHttpUtilsInstance {
        private static final OkHttpUtils instance = new OkHttpUtils();
    }

    public Retrofit getRetrofit() {
        return retrofit;
    }

    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    private Retrofit retrofit;
    private OkHttpClient okHttpClient;

    private OkHttpUtils() {
        okHttpClient = new OkHttpClient.Builder().retryOnConnectionFailure(true)
                .connectTimeout(1000 * 60, TimeUnit.MILLISECONDS)
                .readTimeout(1000 * 60, TimeUnit.MILLISECONDS)
                .writeTimeout(1000 * 60, TimeUnit.MILLISECONDS)
                .addInterceptor(new PublicParamsInterceptor())
                .addInterceptor(new AuthInterceptor())
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL) //设置网络请求的Url地址
                .addConverterFactory(GsonConverterFactory.create()) //设置数据解析器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .build();
    }


    public static OkHttpUtils getInstance() {
        return OkHttpUtilsInstance.instance;
    }

    public Api getApi() {
        return retrofit.create(Api.class);
    }

}
